<?php

class indexController Extends baseController {

    public function index() 
    {
            $order = 1;
            $start = 1;
            $length =10;
            if(isset($_REQUEST["order"]))
            {
                $order = intval($_REQUEST["order"]);
            }
            $tempOrder = " order by create_date DESC ";
            if($order == 2)
            {
                $tempOrder = " order by view DESC ";    
            }
            else if($order == 3)
            {
                $tempOrder = " order by gia_phong_max DESC ";
            }
            else if($order == 4)
            {
                $tempOrder = " order by sophongcontrong DESC ";
            }
            
            //dieu kien where
            $where = "";
            $diadiem = "";
            $gia_phong_min = "";
            $gia_phong_max = "";
            $gia_dien_min = "";
            $gia_dien_max = "";
            $gia_nuoc_min = "";
            $gia_nuoc_max = "";
            $phong_khep_kin = "";
            $gioi_tinh_thue = "";
            $anh_dai_dien = "";
            $gioi_tinh_thue = "";
            $internet = "";
            $gan_duong  = "";
            $gan_cho = "";
            $dien_thoai = "";
            if(isset($_POST["diadiem"]))
            {
                $diadiem = $_POST["diadiem"];
            }
            if(isset($_POST["gia_phong_max"]))
            {
                $gia_phong_max = $_POST["gia_phong_max"];
            }
            if(isset($_POST["gia_dien_min"]))
            {
                $gia_dien_min = $_POST["gia_dien_min"];
            }
            if(isset($_POST["gia_dien_max"]))
            {
                $gia_dien_max = $_POST["gia_dien_max"];
            }
            if(isset($_POST["gia_nuoc_min"]))
            {
                $gia_nuoc_min = $_POST["gia_nuoc_min"];
            }
            if(isset($_POST["gia_nuoc_max"]))
            {
                $gia_nuoc_max = $_POST["gia_nuoc_max"];
            }
            if(isset($_POST["phong_khep_kin"]))
            {
                $phong_khep_kin = $_POST["phong_khep_kin"];
            }
            if(isset($_POST["gioi_tinh_thue"]))
            {
                $gioi_tinh_thue = $_POST["gioi_tinh_thue"];
            }
            if(isset($_POST["anh_dai_dien"]))
            {
                $anh_dai_dien = $_POST["anh_dai_dien"];
            }
            if(isset($_POST["internet"]))
            {
                $internet = $_POST["internet"];
            }
            if(isset($_POST["gan_duong"]))
            {
                $gan_duong = $_POST["gan_duong"];
            }
            if(isset($_POST["gan_cho"]))
            {
                $gan_cho = $_POST["gan_cho"];
            }
            if(isset($_POST["dien_thoai"]))
            {
                $dien_thoai = $_POST["dien_thoai"];
            }
            // end dieu kien where
            $sql = "select * from ".table_prefix("room").$tempOrder;
             $page = 1;
            if(isset($_REQUEST["p"]))
            {
                $page = intval($_REQUEST["p"]);
            }
            $row_total = getRow($sql);
            $page_size = 6;
            $pagegroup_size =5;
            if(isset($_REQUEST["order"]))
            {
                $url="index.php?rt=inex/index&order={$order}";        
            }
            else
            {
                $url="index.php?rt=inex/index";
            }
            
            
        
            
            $roomDAO = CreateObject("Model_DAO_Room");
            $rooms = $roomDAO->lists($order,$page*$page_size-$page_size,$page_size);
            $this->registry->template->page = $page;
            $this->registry->template->row_total = $row_total;
            $this->registry->template->page_size = $page_size;
            $this->registry->template->pagegroup_size = $pagegroup_size;
            $this->registry->template->url = $url;
            $this->registry->template->rooms = $rooms;
            $this->registry->template->roomDAO = $roomDAO;
            
            $this->registry->template->show('index_index');
            
    }
    
    
    public function view(){
        if(isset($_REQUEST["id"])){
            $id = $_REQUEST["id"];
            $roomDAO = CreateObject("Model_DAO_Room");
            $room = $roomDAO->get($id);   
            $this->registry->template->room = $room;
            $this->registry->template->show('index_view');
       }
    }
    
    public function baothongtinsai()
    {
        if(isset($_POST["idRoom"]))
        {
            $idRoom = $_POST["idRoom"];
            $roomDAO = CreateObject("Model_DAO_Room");
            if(isset($_SERVER['REMOTE_ADDR']))
            {
                $ip = $_SERVER['REMOTE_ADDR'];            
                $status = $roomDAO->baothongtinsai($idRoom,$ip);
                if($status)
                    echo "true";
                else
                    echo "false";
            }
            else
                echo "false";
        }       
        echo "false";
    }
    
    public function like(){
        if(isset($_REQUEST["id"])){
            $id = $_REQUEST["id"];
            $roomDAO = CreateObject("Model_DAO_Room");
            $room = $roomDAO->get($id);   
            $this->registry->template->room = $room;
            $this->registry->template->show('index_view');   
        }
    }
    
    public function addSoSanh()
    {
        if(isset($_POST["idRoom"]))
        {
            $idRoom = $_POST["idRoom"];
            $listSoSanh = $_SESSION["listSoSanh"];
            if(count($listSoSanh)<3)
            {
                $status = false;
                foreach($listSoSanh as $item)
                {
                    if($item == $idRoom)
                        $status = true;
                }
                if($status)
                    echo "-2";
                else
                {
                    $listSoSanh[] = $idRoom;
                    $_SESSION["listSoSanh"] = $listSoSanh;
                    echo "1";
                }    
            }
            else
                echo "-1";
        }
    }
    
    public function sosanh()
    {
        if(isset($_SESSION["listSoSanh"]))
        {
            $listSoSanh = $_SESSION["listSoSanh"];
            if(count($listSoSanh)>0)
            {
                $listObjs =  array();
                $roomDAO = CreateObject("Model_DAO_Room");
                foreach($listSoSanh as $item)
                {
                    $obj = $roomDAO->get($item);
                    $listObjs[] = $obj;
                }
                $this->registry->template->listObjs = $listObjs;
            }   
            $this->registry->template->errorCode = "-2";
        }
        else
            $this->registry->template->errorCode = "-1";
        
        $this->registry->template->show("index_compare");
    }
    
    public function comment()
    {
        try
        {
            if(isset($_REQUEST["idRoom"]))
            {
                $idRoom = $_POST["idRoom"];
                $comment_email = $_POST["comment_email"];
                $comment_name = $_POST["comment_name"];
                $comment_text = $_POST["comment_text"];
                $comment_title = $_POST["comment_title"];
                $commentDAO = CreateObject("Model_DAO_Comment");
                $status = $commentDAO->commentRoom($idRoom,$comment_title,$comment_text,$comment_name,$comment_email);
                if($status)
                    echo "true";
                else
                    echo "false";
            }
            else
            {
                echo  "false";
            }
        }
        catch(MyException $ex)
        {
            echo "false";  
        }
        
    }
}
?>